using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for MapsControl.
	/// </summary>
	public class MapsControl : AdminUserControl
	{
    private ExtendedDataGrid mapsGrid;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private IContainer components;

		public MapsControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the list", new ActionExecuteDelegate(RefreshList));
      actionList.Add("Copy to clipboard", "Copy the list to the clipboard", new ActionExecuteDelegate(CopyToClipboard));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.mapsGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.mapsGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // mapsGrid
      // 
      this.mapsGrid.AllowNavigation = false;
      this.mapsGrid.CaptionVisible = false;
      this.mapsGrid.DataMember = "";
      this.mapsGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.mapsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.mapsGrid.Location = new System.Drawing.Point(0, 0);
      this.mapsGrid.Name = "mapsGrid";
      this.mapsGrid.Size = new System.Drawing.Size(426, 384);
      this.mapsGrid.TabIndex = 0;
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(426, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 384);
      this.uiActionsControl.TabIndex = 1;
      // 
      // MapsControl
      // 
      this.Controls.Add(this.mapsGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "MapsControl";
      this.Size = new System.Drawing.Size(576, 384);
      ((System.ComponentModel.ISupportInitialize)(this.mapsGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private MapCollection maps;
    public void LoadMaps(MapCollection maps)
    {
      this.maps = maps;
      mapsGrid.DataSource = null;
      mapsGrid.DataSource = maps;
      mapsGrid.ClearColumns();
      mapsGrid.AddColumn("Name");
      mapsGrid.AddColumn("ProcedureName", "Procedure name");
      mapsGrid.AddColumn("TableName", "Table name");
      mapsGrid.AddColumn("LimitAccessTo", "Limit access to");
      mapsGrid.AddColumn("Prefix");
      mapsGrid.AddColumn("SuffixSize", "Suffix size");
      mapsGrid.AddColumn("Subject");
      if (maps.Server.SchemaVersion >= SchemaVersion.Version75)
      {
        mapsGrid.AddColumn("DelegateEvents", "Delegate events");
        mapsGrid.AddColumn("DelegateEventsBeforeLocal", "Delegate events before local");
      }
      mapsGrid.AutosizeColumns();
    }

    private string RefreshList()
    {
      maps.Refresh();
      LoadMaps(maps);
      OnRefreshRequired();
      return null;
    }

    private string CopyToClipboard()
    {
      mapsGrid.CopyToClipboard();
      return null;
    }
	}
}
